package com.demo.controller;

import com.demo.bean.SmsCode;
import com.demo.common.Result;
import com.demo.service.JetCacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试jetCache缓存
 */
@Api(tags = "测试jetCache缓存")
@RestController
@RequestMapping("JetCache")
public class JetCacheController {

    @Autowired
    private JetCacheService jetCacheService;

    @ApiOperation(value = "获得验证码", notes = "获得验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String")
    })
    @GetMapping("getCode")
    public Result getCode(String phone) {
        return  Result.ok().success(jetCacheService.getCode(phone));
    }


    @ApiOperation(value = "比对验证码", notes = "比对验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String")
    })
    @GetMapping("checkCode")
    public Result checkCode(SmsCode smsCode) {
        if (jetCacheService.checkCode(smsCode)) {
            return Result.ok("比对成功");
        } else {
            return Result.error("比对失败");
        }
    }
}
